package org.oslims.client.uicomponents.editbox;

import java.util.HashMap;
import java.util.Map;

import org.oslims.client.uicomponents.inputfields.InputField;
import org.oslims.client.uicomponents.inputfields.TextInputField;
import org.oslims.domain.Department;

import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EditDepartmentBox {

	final DialogBox editDepartmentBox = new DialogBox(false, true);
	final VerticalPanel editDepartmentBoxPanel = new VerticalPanel();

	protected Map<String, InputField> inputFields = new HashMap<String, InputField>();

	final HorizontalPanel buttonLine = new HorizontalPanel();
	final Button saveButton = new Button("Save");
	final Button cancelButton = new Button("Cancel");

	final TextInputField nameField = new TextInputField("name", "Name: ", "80px", "200px");
	final TextInputField abbreviationField = new TextInputField("abbreviation", "Abbreviation: ", "80px", "200px");

	private Department department;

	String editedType;

	public EditDepartmentBox() {

		editDepartmentBoxPanel.add(nameField);
		inputFields.put(nameField.getFieldName(), nameField);

		editDepartmentBoxPanel.add(abbreviationField);
		inputFields.put(abbreviationField.getFieldName(), abbreviationField);

		buttonLine.add(saveButton);
		buttonLine.add(cancelButton);

		editDepartmentBox.add(editDepartmentBoxPanel);

		// Add the save and cancel buttons
		editDepartmentBoxPanel.add(buttonLine);
	}

	public void hide() {
		editDepartmentBox.hide();
	}

	public void clear() {
		department = null;
		nameField.getInputField().setValue("");
		abbreviationField.getInputField().setValue("");
	}

	public void showNew() {
		this.clear();
		this.clearAllErrors();
		editDepartmentBox.setText("Create new Department");
		editDepartmentBox.center();
	}

	public void showEdit(Department p) {
		this.clear();
		this.clearAllErrors();

		department = p;
		nameField.getInputField().setValue(p.getName());
		abbreviationField.getInputField().setValue(p.getAbbreviation());

		editDepartmentBox.setText("Edit Department");
		editDepartmentBox.center();
	}

	public void showError(String fieldName, String message) {
		inputFields.get(fieldName).showError(message);
	}

	public void clearError(String fieldName) {
		inputFields.get(fieldName).clearError();
	}

	public void clearAllErrors() {
		for (InputField inputField : inputFields.values()) {
			inputField.clearError();
		}
	}

	public HasValue<String> getName() {
		return nameField.getInputField();
	}

	public HasValue<String> getUnit() {
		return abbreviationField.getInputField();
	}

	public HasClickHandlers getSaveButton() {
		return saveButton;
	}

	public HasClickHandlers getCancelButton() {
		return cancelButton;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Department getDepartment() {
		return department;
	}
}
